<template>
  <div class="app-container">
    <el-form :model="queryParams" ref="queryForm" :inline="true" v-show="showSearch" label-width="68px">
      <el-form-item label="股票名称" prop="stockName">
        <el-input
          v-model="queryParams.stockName"
          placeholder="请输入股票名称"
          clearable
          size="small"
          @keyup.enter.native="handleQuery"
        />
      </el-form-item>
      <el-form-item label="股票发行人" prop="stockOwnername">
        <el-input
          v-model="queryParams.stockOwnername"
          placeholder="请输入股票发行人"
          clearable
          size="small"
          @keyup.enter.native="handleQuery"
        />
      </el-form-item>
      <el-form-item label="审核状态" prop="feedbackVerify">
        <el-select v-model="queryParams.feedbackVerify" placeholder="请选择审核状态" clearable size="small">
          <el-option
            v-for="dict in feedbackVerifyOptions"
            :key="dict.dictValue"
            :label="dict.dictLabel"
            :value="parseInt(dict.dictValue)"
          />
        </el-select>
      </el-form-item>
      <el-form-item label="投资人" prop="createBy">
        <el-input
          v-model="queryParams.createBy"
          placeholder="请输入投资人"
          clearable
          size="small"
          @keyup.enter.native="handleQuery"
        />
      </el-form-item>
      <el-form-item>
        <el-button type="primary" icon="el-icon-search" size="mini" @click="handleQuery">搜索</el-button>
        <el-button icon="el-icon-refresh" size="mini" @click="resetQuery">重置</el-button>
      </el-form-item>
    </el-form>

    <el-table v-loading="loading" :data="approvalList" @selection-change="handleSelectionChange">
      <el-table-column type="selection" width="55" align="center" />
      <el-table-column label="股票代码" align="center" prop="stockCode" />
      <el-table-column label="股票名称" align="center" prop="stockName" />
      <el-table-column label="发行人反馈评价" align="center" prop="feedbackEvaluate" :formatter="feedbackEvaluateFormat" />
      <el-table-column label="审核状态" align="center" prop="feedbackVerify" :formatter="feedbackVerifyFormat" />
      <el-table-column label="投资人" align="center" prop="createBy" />
      <el-table-column label="操作" align="center" class-name="small-padding fixed-width">
        <template slot-scope="scope">
          <el-button
            v-if="scope.row.feedbackVerify === 1"
            size="mini"
            type="text"
            icon="el-icon-edit"
            @click="handleUpdate(scope.row)"
          >审核</el-button>
        </template>
      </el-table-column>
    </el-table>

    <pagination
      v-show="total>0"
      :total="total"
      :page.sync="queryParams.pageNum"
      :limit.sync="queryParams.pageSize"
      @pagination="getList"
    />

    <!-- 添加或修改反馈审核对话框 -->
    <el-dialog :visible.sync="open" append-to-body title="审核" width="800px">
      <el-form ref="form" :model="form" :rules="rules" label-width="80px">
        <el-descriptions :column="1" title="发行信息">
          <el-descriptions-item label="股票代码">{{ form.stockCode }}</el-descriptions-item>
          <el-descriptions-item label="股票名称">{{ form.stockName }}</el-descriptions-item>
          <el-descriptions-item label="当前股票价值">{{ form.latestPrice }}</el-descriptions-item>
          <el-descriptions-item label="发行人分红状态">{{feedbackDividendFormat(form) }}</el-descriptions-item>
          <el-descriptions-item label="发行人资金使用情况">{{ feedbackMoneytrackFormat(form) }}</el-descriptions-item>
          <el-descriptions-item label="发行人反馈评价">{{ feedbackEvaluateFormat(form) }}</el-descriptions-item>
          <el-descriptions-item label="反馈评价详细描述">{{ form.feedbackMessage }}</el-descriptions-item>
        </el-descriptions>

        <div class="el-descriptions-title">
          审核意见
        </div>
        <el-form-item label="是否处罚" prop="field101">
          <el-radio-group v-model="radioTreaty" @change="agreeChange">
            <el-radio label="1" border>不处罚</el-radio>
            <el-radio label="2" border>处罚</el-radio>
          </el-radio-group>

        </el-form-item>

        <el-form-item label="处罚" prop="treaty">
          <el-input
            size="small"
            clearable
            v-model="form.treaty"
            :disabled="btnstatus"
            placeholder="处罚股票价值"
          />
        </el-form-item>

        <div class="margin-top">
          <el-form-item label="审核描述" prop="remark">
            <el-input v-model="form.remark" placeholder="请输入内容" type="textarea"/>
          </el-form-item>
        </div>
      </el-form>
      <div slot="footer" class="dialog-footer">
        <el-button @click="cancel">关 闭</el-button>
        <el-button @click="submitForm">驳回</el-button>
        <el-button type="primary" @click="submit">通过</el-button>
      </div>
    </el-dialog>
  </div>
</template>

<script>
import { listApproval, getApproval, delApproval, addApproval, updateApproval, exportApproval } from "@/api/operate/feedback/approval";
import { listStock, getStock, updateStock} from "@/api/operate/stock";
export default {
  name: "Approval",
  data() {
    return {
      // 遮罩层
      loading: true,
      // 导出遮罩层
      exportLoading: false,
      // 选中数组
      ids: [],
      // 非单个禁用
      single: true,
      // 非多个禁用
      multiple: true,
      // 显示搜索条件
      showSearch: true,
      // 总条数
      total: 0,
      // 反馈审核表格数据
      approvalList: [],
      // 弹出层标题
      title: "",
      // 是否显示弹出层
      open: false,
      // 发行人分红状态字典
      feedbackDividendOptions: [],
      // 发行人资金使用情况字典
      feedbackMoneytrackOptions: [],
      // 发行人反馈评价字典
      feedbackEvaluateOptions: [],
      // 审核状态字典
      feedbackVerifyOptions: [],
      radioTreaty: '1',
      btnstatus:true,
      treaty: null,
      //股票对象
      mystrock: null,
      // 查询参数
      queryParams: {
        pageNum: 1,
        pageSize: 10,
        stockCode: null,
        stockName: null,
        stockOwnerid: null,
        stockOwnername: null,
        feedbackDividend: null,
        feedbackMoneytrack: null,
        feedbackEvaluate: null,
        feedbackMessage: null,
        feedbackVerify: 1,
        deptId: null,
        createById: null,
        updateById: null,
        treaty: null,
        latestPrice: 0,
      },
      // 表单参数
      form: {},
      // 表单校验
      rules: {
        feedbackVerify: [
          { required: true, message: "审核状态不能为空", trigger: "change" }
        ],
      }
    };
  },
  created() {
    this.getList();
    this.getDicts("feedback_dividend").then(response => {
      this.feedbackDividendOptions = response.data;
    });
    this.getDicts("feedback_moneytrack").then(response => {
      this.feedbackMoneytrackOptions = response.data;
    });
    this.getDicts("feedback_evaluate").then(response => {
      this.feedbackEvaluateOptions = response.data;
    });
    this.getDicts("feedback_verify").then(response => {
      this.feedbackVerifyOptions = response.data;
    });
  },
  methods: {
    /** 查询反馈审核列表 */
    getList() {
      this.loading = true;
      listApproval(this.queryParams).then(response => {
        this.approvalList = response.rows;
        this.total = response.total;
        this.loading = false;
      });

    },
    // 发行人分红状态字典翻译
    feedbackDividendFormat(row, column) {
      return this.selectDictLabel(this.feedbackDividendOptions, row.feedbackDividend);
    },
    // 发行人资金使用情况字典翻译
    feedbackMoneytrackFormat(row, column) {
      return this.selectDictLabel(this.feedbackMoneytrackOptions, row.feedbackMoneytrack);
    },
    // 发行人反馈评价字典翻译
    feedbackEvaluateFormat(row, column) {
      return this.selectDictLabel(this.feedbackEvaluateOptions, row.feedbackEvaluate);
    },
    // 审核状态字典翻译
    feedbackVerifyFormat(row, column) {
      return this.selectDictLabel(this.feedbackVerifyOptions, row.feedbackVerify);
    },
    // 取消按钮
    cancel() {
      this.open = false;
      this.reset();
    },
    // 表单重置
    reset() {
      this.form = {
        id: null,
        stockCode: null,
        stockName: null,
        stockOwnerid: null,
        stockOwnername: null,
        feedbackDividend: 0,
        feedbackMoneytrack: 0,
        feedbackEvaluate: 0,
        feedbackMessage: null,
        feedbackVerify: 1,
        deptId: null,
        createBy: null,
        createById: null,
        createTime: null,
        updateBy: null,
        updateById: null,
        updateTime: null,
        remark: null,
        latestPrice: 0,
        treaty: null,
        mystrock: null
      };
      this.resetForm("form");
    },
    /** 搜索按钮操作 */
    handleQuery() {
      this.queryParams.pageNum = 1;
      this.getList();
    },
    /** 重置按钮操作 */
    resetQuery() {
      this.resetForm("queryForm");
      this.handleQuery();
    },
    // 多选框选中数据
    handleSelectionChange(selection) {
      this.ids = selection.map(item => item.id)
      this.single = selection.length!==1
      this.multiple = !selection.length
    },
    /** 新增按钮操作 */
    handleAdd() {
      this.reset();
      this.open = true;
      this.title = "添加反馈审核";
    },
    /** 审核按钮操作 */
    async handleUpdate(row) {
      this.reset();
      const id = row.id || this.ids
      let detail = await getApproval(id);
      if (detail && detail.code === 200) {
        this.form = detail.data;
        let stockdetail = await listStock({
          code: detail.stockCode
        });

        if (stockdetail && stockdetail.code === 200 && stockdetail.total > 0) {
          this.form.latestPrice = stockdetail.rows[0].latestPrice;
          this.mystrock = stockdetail.rows[0];
          console.log(this.mystrock)
        }
      }

      // getApproval(id).then(async response => {
      //   this.form = await response.data;
      //   const stockcode = this.form.stockCode;
      //   await listStock({
      //     code: stockcode
      //   }).then(async response => {
      //     console.log(response)
      //     _this.form.latestPrice = await response.rows[0].latestPrice;
      //   });
      // });

      this.open = true;
      this.title = "进行审核";
    },
    //进行审核
    submit() {
      this.form.feedbackVerify = "2";
      this.$refs["form"].validate(valid => {
        if (valid) {
          if (this.form.treaty !== 0) {
            this.mystrock.latestPrice -= this.form.treaty;
            console.log( this.mystrock.latestPrice)
            updateStock(this.mystrock).then(response => {
            });
            updateApproval(this.form).then(response => {
            this.msgSuccess("审核通过");
            this.open = false;
            this.getList();
          });
          }
        }
      });
    },
    /** 驳回按钮 */
    submitForm() {
      this.form.feedbackVerify = "3";
      this.$refs["form"].validate(valid => {
        if (valid) {
          updateApproval(this).then(response => {
            this.msgSuccess("已驳回");
            this.open = false;
            this.getList();
          });
        }
      });
    },
    /** 删除按钮操作 */
    handleDelete(row) {
      const ids = row.id || this.ids;
      this.$confirm('是否确认删除反馈审核编号为"' + ids + '"的数据项?', "警告", {
          confirmButtonText: "确定",
          cancelButtonText: "取消",
          type: "warning"
        }).then(function() {
          return delApproval(ids);
        }).then(() => {
          this.getList();
          this.msgSuccess("删除成功");
        }).catch(() => {});
    },
// 切换处罚
    agreeChange(val){
      let that = this
      that.btnstatus=(val==='1')?true:false;
    },
  }
};

</script>

<style scoped>
  .box-card {
    margin: 20px 0;
  }

  .el-descriptions-title {
    margin-top: 40px;
    margin-bottom: 20px;
    font-size: 16px;
    font-weight: bold;
  }

  .margin-top {
    margin-top: 20px;
  }
  </style>
